package fm.dongman.modellib.remote;

import android.util.Log;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import fm.dongman.animefm.contractlib.contract.FSheetContract;
import fm.dongman.animefm.contractlib.model.FSheetDataModel;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.local.FSheetLocalDataSource;

/**
 * Sheet网络获取
 * Created by KUIGE on 2017/10/15.
 */

public class FSheetRemoteDataSource implements FSheetContract.IFSheetDataSource {
    private static FSheetRemoteDataSource SINGLETON; // 实例

    private RemoteOperationHelper mRemote;// 网络解析对象

    private boolean isRemote =true;

    private FSheetRemoteDataSource() {
        mRemote = RemoteOperationHelper.getOperationHelper();
    }

    public static FSheetRemoteDataSource getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new FSheetRemoteDataSource();
        }
        return SINGLETON;
    }

    @Override
    public void getData(@NotNull LoadSourceCallback<? super FSheetDataModel> callback) {
        if (isRemote) {
            JSONObject jsonObject = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.FIND_SHEET_REMOTE + "page=1&mandan_num=12&daren_num=8&topic_num=5");
            if (jsonObject != null) {
                callback.onDataLoaded(mRemote.downMandan(jsonObject));
            } else callback.onDataLoadFiled();
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(FSheetDataModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
